// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

import './assets/css/reset.css'
import './assets/css/border.css'
import './assets/css/animate.css'
import './assets/iconfont/iconfont.css'
import './assets/css/weui.min.css'
import './assets/css/common.styl'
import common from './utils/common'
import store from './store'

// eslint-disable-next-line
// import vconsole from './utils/vconsole'
// eslint-disable-next-line
// import axios from 'axios'

// vant
import { Tab, Tabs, List, PullRefresh, SwipeCell, Toast, Icon, Loading, Tag, Dialog, Button, Switch, Row, Col, Radio, RadioGroup, Actionsheet, Cell, CellGroup, DatetimePicker } from 'vant'
import 'vant/lib/tabs/index.css'
// import 'vant/lib/tab/index.css'
import 'vant/lib/list/index.css'
import 'vant/lib/pull-refresh/index.css'
import 'vant/lib/swipe-cell/index.css'
import 'vant/lib/toast/index.css'
import 'vant/lib/icon/index.css'
import 'vant/lib/loading/index.css'
import 'vant/lib/tag/index.css'
import 'vant/lib/button/index.css'
import 'vant/lib/dialog/index.css'
import 'vant/lib/switch/index.css'
import 'vant/lib/row/index.css'
import 'vant/lib/col/index.css'
import 'vant/lib/radio/index.css'
import 'vant/lib/actionsheet/index.css'
import 'vant/lib/cell/index.css'
import 'vant/lib/cell-group/index.css'
import 'vant/lib/picker/index.css'

// import fastClick from 'fastclick'
// fastClick.attach(document.body)

Vue.use(Tab).use(Tabs).use(List).use(PullRefresh).use(SwipeCell).use(Toast).use(Icon).use(Loading).use(Tag).use(Dialog).use(Button).use(Switch).use(Row).use(Col).use(Radio).use(RadioGroup).use(Actionsheet).use(Cell).use(CellGroup).use(DatetimePicker)

Vue.prototype.cm = common

Vue.config.productionTip = false

// eslint-disable-next-line
function wxAuthor () {
  common.myajax('/api/login/getLoingUrl', {}, (data) => {
    // console.log(JSON.stringify(data))
    if (data.type === 'url') {
      console.log('跳转到微信授权' + data.url)
      // 前端调试可以直接打开这个请求
      // location.replace('http://front.sys360.cn/static/back.html?code=aasdfasd&state=jjjlsl&test=1')

      /**
       * @editor mashizhen
       * 2019-07-14 10:46:54
       */

      // location.replace('http://172.16.12.10:8080/static/back.html?code=aasdfasd&state=jjjlsl&test=1')
      location.replace('http://47.106.252.199:9000/static/back.html?code=aasdfasd&state=jjjlsl&test=1')
      // location.replace('http://127.0.0.1:8080/static/back.html?code=aasdfasd&state=jjjlsl&test=1')
      // alert('a:' + data.url)
      location.replace(data.url)
      // location.href = (data.url)
    }
  })
}

router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  localStorage.curpath = to.path

  if (!sessionStorage.userId) {
    document.title = ''
    // 未登录
    if (to.path === '/') {
      // 如果是首页 判断有没有code
      let query = to.query
      if (query.code && query.state) {
        // 如果跳转到首页的查询中包含 code 和state ,则进行登录操作
        common.myajax('/api/login/getUserId', {'code': query.code, 'state': query.state, 'test': query.test}, (data) => {
          // 正常登录 获得 微信用户id 并保存到本地存储中
          console.log('uuuuuuuuuuuuuuuuuuuuuuu:' + JSON.stringify(data))
          sessionStorage.userId = data.user
          store.commit('set_user_info', data)
          localStorage.rolecode = data.roleCode ? data.roleCode : '4'
          localStorage.roletype = data.roleType ? data.roleType : '1'
          // console.log('auth,after path:' + localStorage.authfrom)

          // localStorage.roletype = (data.roleCode === '4') ? '0' : '1'
          // if (data.roleCode !== '4' && localStorage.authfrom === '/') {
          if (localStorage.rolecode !== '4' && localStorage.roletype === '1' && localStorage.authfrom === '/') {
            localStorage.authfrom = '/mindex'
          }

          next({
            path: localStorage.authfrom
          })
        })
      } else {
        // 如果不存在参数 则进行微信授权登录
        localStorage.authfrom = to.path
        console.log('auth,before path:' + localStorage.authfrom)
        wxAuthor()
      }
    } else {
      localStorage.authfrom = to.path
      console.log('auth,before path:' + localStorage.authfrom)
      wxAuthor()
    }
  } else {
    if (to.meta.title) {
      document.title = to.meta.title
    }
    next()
  }
  // next()
})

// 初始化一些数据
store.dispatch('getCategoryInfo')
store.dispatch('getBuildingInfo')
store.dispatch('getHolidays')

// common.myajax('/api/login/test', {
//   mobile: '18502741642'
//   // mobile: '17095133327'
//   // mobile: '15311001636'
//   // userName: 'leader' // user
// }, (data) => {
//   sessionStorage.userId = data.user
//   store.commit('set_user_info', data)
//   localStorage.rolecode = data.roleCode ? data.roleCode : '4'
//   localStorage.roletype = data.roleType ? data.roleType : '1'

//   // if (localStorage.rolecode !== '4' && localStorage.roletype === '1') {
//   //   alert('/mindex')
//   // }
// })

// 回退事件处理
window.addEventListener('popstate', function (e) {
  let urlarr = location.href.split('#')
  let preurl = urlarr[0] + '#'
  let gopath = '/'
  let curpath = localStorage.curpath
  if (curpath === '/' || curpath === '/mindex') {
    if (typeof window.WeixinJSBridge === 'undefined') {
      document.addEventListener('WeixinJSBridgeReady', function () {
        window.WeixinJSBridge.call('closeWindow')
      }, false)
    } else {
      window.WeixinJSBridge.call('closeWindow')
    }
  } else {
    if (curpath === '/me' || curpath === '/opersucc' || curpath === '/operfail') {
      gopath = (localStorage.rolecode !== '4' && localStorage.roletype === '1') ? '/mindex' : '/'
      window.history.pushState({}, '', preurl + gopath)
    } else if (curpath.indexOf('/mreportdetail') >= 0) {
      gopath = localStorage.morderdetailfrom
      window.history.pushState({}, '', preurl + gopath)
    } else {
      for (const cur in common.lastPages) {
        if (common.lastPages.hasOwnProperty(cur) && curpath.indexOf(cur) >= 0) {
          gopath = common.lastPages[cur]
          window.history.pushState({}, '', preurl + gopath)
          break
        }
      }
    }
  }
}, false)

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
